package io.terminus.spzx.common.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.terminus.spzx.consts.UserInfoConstant;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc OpenFeign调用传递request header的token的拦截器
 * @Date 2024-12-08
 */
@Component
public class UserTokenFeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader(UserInfoConstant.USERINFO_TOKEN_REQUEST_HEADER);

        requestTemplate.header(UserInfoConstant.USERINFO_TOKEN_REQUEST_HEADER, token);
    }
}
